
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/common/util/datetime.dart';
import 'package:xim/common/util/system.dart';
import 'package:xim/http/apis/secret/secret_api.dart';
import 'package:xim/http/apis/secret/secret_model.dart';
import 'package:xim/widget/common/state.dart';
import 'package:xim/widget/common/ui/cell.dart';

class SessionSecretKeyDetailPage extends StatefulWidget {

  final int secretKeyId;

  const SessionSecretKeyDetailPage({Key? key, required this.secretKeyId}) : super(key: key);

  @override
  State<SessionSecretKeyDetailPage> createState() => _SessionSecretKeyDetailPageState();
}

class _SessionSecretKeyDetailPageState extends AppBarState<SessionSecretKeyDetailPage>
    with ShimmerState<SessionSecretKeyDetailPage>, BrightnessModeState<SessionSecretKeyDetailPage>  {

  final ModelColor appBarColor = ModelColor(lightColor:const Color(0xffededed),darkColor: const Color(0xff111111));
  final ModelColor bgColor = ModelColor(lightColor:const Color(0xffffffff),darkColor: const Color(0xff181818));
  final ModelColor textColor = ModelColor(lightColor:const Color(0xff171717),darkColor: const Color(0xffcfcfcf));

  @override
  String get appBarTitle => "密钥详情";

  SessionSecretKeyModel? secretKeyModel;

  @override
  List<ModelColor> get brightnessColors => [bgColor,appBarColor,textColor];

  @override
  Color get appBarBgColor => appBarColor.color;

  @override
  TextStyle get appBarTitleStyle => TextStyle(color: textColor.color);

  @override
  void initContextDate(context) async {
    super.initContextDate(context);
    initModelColor(context);
  }

  @override
  Widget widgetBuild(BuildContext context) {
    return Scaffold(
      appBar: appBar,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: bgColor.color,
        padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 8),
        child: Column(
          children: [
            Cell.titleCell(
              title: "密钥",
              titleStyle: TextStyle(color: textColor.color),
              rightItem: Text(secretKeyModel?.secretKey.ellipsis(12)??'', style: TextStyle(color: textColor.color),),
            ),
            const SizedBox(height: 6,),
            Cell.titleCell(
              title: "版本",
              titleStyle: TextStyle(color: textColor.color),
              rightItem: Text(secretKeyModel?.secretKeyVersion.toString()??'', style: TextStyle(color: textColor.color)),
            ),
            const SizedBox(height: 6,),
            Cell.titleCell(
              title: "创建者",
              titleStyle: TextStyle(color: textColor.color),
              rightItem: Text(secretKeyModel?.creatorInfo?.name??'', style: const TextStyle(color: StyleColor.textLinkColor)),
              onTab: (){
                if(secretKeyModel?.creatorInfo?.account!=null){
                  context.push("/profile/public/${secretKeyModel?.creatorInfo?.account}");
                }
              },
            ),
            const SizedBox(height: 6,),
            Cell.titleCell(
              title: "创建时间",
              titleStyle: TextStyle(color: textColor.color),
              rightItem: Text(secretKeyModel?.createTime.format("yyyy-mm-dd")??'', style:TextStyle(color: textColor.color)),
            ),
            const SizedBox(height: 6,),
            Cell.titleCell(
              title: "使用频率",
              titleStyle: TextStyle(color: textColor.color),
              rightItem: Text('高(60%)', style:TextStyle(color: textColor.color)),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Future<void> initData() async {
    var secretKey = await SecretApi.querySessionSecretKeyById(widget.secretKeyId);
    setState(() {
      secretKeyModel = secretKey;
    });
    return super.initData();
  }
}
